package com.tz.farmer.controller;

import com.smartPark.msframe.result.ResultEx;
import com.tz.farmer.service.IFarmerSunDataService;
import com.tz.farmer.so.DefaultSo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.TreeMap;

@Slf4j
@RestController
@RequestMapping("/consume")
@Api(tags = "阳光消费接口")
public class ConsumeController {

    private static final String scanKey = "scanKey";
    private static final String rumorKey = "rumorKey";
    private static final String checkKey = "checkKey";
    private static final String safetyKey = "safetyKey";
    private static final String activityKey = "activityKey";
    private static final String nutritionKey = "nutritionKey";
    private static final String market = "阳光消费";

    @Autowired
    private IFarmerSunDataService dataService;

    @PostMapping("/set-scan")
    @ApiOperation(value = "设置扫码次数")
    public ResultEx setScanConfig(@RequestBody DefaultSo body) {
        if (body == null || ObjectUtils.isEmpty(body.getCount())) {
            return ResultEx.builder().makeFailedResult("缺少数量count");
        }
        dataService.setData(market, scanKey, body.getCount());

        return ResultEx.builder().makeSuccessResult();
    }

    @PostMapping("/set-rumor")
    @ApiOperation(value = "设置辟谣助手")
    public ResultEx setRumorConfig(@RequestBody DefaultSo body) {
        if (body == null || ObjectUtils.isEmpty(body.getCount())) {
            return ResultEx.builder().makeFailedResult("缺少数量count");
        }
        dataService.setData(market, rumorKey, body.getCount());

        return ResultEx.builder().makeSuccessResult();
    }

    @PostMapping("/set-check")
    @ApiOperation(value = "设置你点我检数")
    public ResultEx setCheckConfig(@RequestBody DefaultSo body) {
        if (body == null || ObjectUtils.isEmpty(body.getCount())) {
            return ResultEx.builder().makeFailedResult("缺少数量count");
        }
        dataService.setData(market, checkKey, body.getCount());

        return ResultEx.builder().makeSuccessResult();
    }

    @PostMapping("/set-safety")
    @ApiOperation(value = "设置食安知识")
    public ResultEx setSafetyConfig(@RequestBody DefaultSo body) {
        if (body == null || ObjectUtils.isEmpty(body.getCount())) {
            return ResultEx.builder().makeFailedResult("缺少数量count");
        }
        dataService.setData(market, safetyKey, body.getCount());

        return ResultEx.builder().makeSuccessResult();
    }

    @PostMapping("/set-activity")
    @ApiOperation(value = "设置食安活动")
    public ResultEx setActivityConfig(@RequestBody DefaultSo body) {
        if (body == null || ObjectUtils.isEmpty(body.getCount())) {
            return ResultEx.builder().makeFailedResult("缺少数量count");
        }
        dataService.setData(market, activityKey, body.getCount());

        return ResultEx.builder().makeSuccessResult();
    }

    @PostMapping("/set-nutrition")
    @ApiOperation(value = "设置营养推荐")
    public ResultEx setNutritionConfig(@RequestBody DefaultSo body) {
        if (body == null || ObjectUtils.isEmpty(body.getCount())) {
            return ResultEx.builder().makeFailedResult("缺少数量count");
        }
        dataService.setData(market, nutritionKey, body.getCount());

        return ResultEx.builder().makeSuccessResult();
    }

    @GetMapping("/get")
    @ApiOperation(value = "获取厨房数据")
    public ResultEx getAllConfig() {
        Map<String, Object> response = new TreeMap<>();

        response.put("scan", dataService.getData(market, scanKey));
        response.put("check", dataService.getData(market, checkKey));
        response.put("nutrition", dataService.getData(market, nutritionKey));
        response.put("activity", dataService.getData(market, activityKey));
        response.put("rumor", dataService.getData(market, rumorKey));
        response.put("safety", dataService.getData(market, safetyKey));

        return ResultEx.builder().makeSuccessResult(response);
    }
}
